// Generate random 4 digit PIN
// Date 23:41 12/03/2017
// By Ben a.k.a DreamVB

#include <string>
#include <iostream>
#include <time.h>

using namespace std;
using std::cout;
using std::endl;

string RandomPIN(int length){
	string sDigits = "0123456789";
	string buff = "";
	int i = 0;
	int r = 0;

	//Get random seed;
	while (i < length){
		//Get random char position from sDigits.
		r = rand() % strlen(sDigits.c_str());
		//Build PIN.
		buff += sDigits[r];
		//INC counter
		i++;
	}
	//Return PIN
	return buff;
}


int main(int argc, char *argv[])
{
	srand(time(0));
	bool IsRunning = true;
	bool IsError = false;
	char c = 'Y';

	while (IsRunning){
		//Print out the random PIN
		system("CLS");
		cout << "##############" << endl;
		cout << "#    " << RandomPIN(4).c_str() << "    #" << endl;
		cout << "##############" << endl;
		//Ask user if they want to create a new PIN
		cout << "Would you like a new PIN (Y/N) : ";
		//Get input
		std::cin >> c;
		//Convert to uppercase
		c = toupper(c);

		if (c == 'N'){
			//Exit program.
			IsRunning = false;
			cout << "Thanks for using the program." << endl;
			break;
		}
		if (c != 'Y'){
			//Exit program with error
			cout << "Invalid key press." << endl;
			IsRunning = false;
			IsError = true;
		}
	}

	if (!IsError){
		exit(1);
	}
	else{
		return EXIT_SUCCESS;
	}

}